import { computed, onUnmounted, ref } from 'vue'
import dayjs from 'dayjs'

export const useCountDown = () => {
  const time = ref()
  let timer = null
  const formateTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))

  const start = (CurrentTime) => {
    time.value = CurrentTime

    timer = setInterval(() => {
      time.value--
    }, 1000)
  }
  // 组件销毁时，清除定时器，防止内存泄漏
  onUnmounted(() => {
    timer && clearInterval(timer)
  })
  return {
    start,
    formateTime
  }
}
